/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package educar.abmAlumno;

import educar.abmAlumno.AgregarAlumnoView;
import educar.abmAlumno.AgregarAlumnoController;
import educar.models.Alumno;
import educar.models.User;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author Matias
 */
public class ModificarAlumnoController implements ActionListener {

    private AgregarAlumnoView agregarAlumnoView;
    private String vector[] = new String[4];    
    private AlumnoController alumnoControl;
    private int fila;
    private String nroAlumno;
    private String nombreyApellido;
    
// <editor-fold defaultstate="collapsed" desc="Constructor">
    public ModificarAlumnoController(AlumnoController alumnoController){        
        alumnoControl = alumnoController;
        agregarAlumnoView = new AgregarAlumnoView();
        agregarAlumnoView.setActionListener(this);
    }
 // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Action setView">    
    public void setView(Vector registro, int fil) {
        fila = fil;
        nroAlumno = registro.elementAt(1).toString();
        nombreyApellido = registro.elementAt(3).toString();
        agregarAlumnoView.setTitulo("Modificar un Alumno");
        agregarAlumnoView.setnroAlumno(registro.elementAt(1).toString());
        agregarAlumnoView.setfDni(registro.elementAt(2).toString());
        agregarAlumnoView.setNombreYApellido(registro.elementAt(3).toString());
        agregarAlumnoView.setDireccion((String) registro.elementAt(4));
        agregarAlumnoView.setVisible(true);
    }
// </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Metodos ActionPermormed">     
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (agregarAlumnoView.getJbCancelar() == source){
            agregarAlumnoView.dispose();
        }
        if (agregarAlumnoView.getJbAceptar() == source){
            vector[0] = agregarAlumnoView.getNroAlumno(); 
            vector[1] = agregarAlumnoView.getDni();
            vector[2] = agregarAlumnoView.getNombreYApellido();
            vector[3] = agregarAlumnoView.getDireccion();
            if (!((vector[0].length() == 0) || (vector[1].length() == 0) || (vector[2].length() == 0) || (vector[3].length() == 0))) {
                Alumno alumno = new Alumno(vector); 
                User user = new User(vector[2], vector[1], "Alumno");
                if (alumno.setAlumnoByAlumnoname(nroAlumno) && user.setUserByUsername(nombreyApellido)) {                             
                            alumnoControl.actualizarFila(vector, fila);
                            agregarAlumnoView.dispose();
                } else {
                    JOptionPane.showMessageDialog(null,"Ya existe un registro con el numero de alumno "+ agregarAlumnoView.getNroAlumno()+".","proyecto 2012",JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null,"Existe al menos un campo sin completar.\n\nRevise los campos ingresados","proyecto 2012",JOptionPane.WARNING_MESSAGE);
            }
        }
    }
// </editor-fold>
}
